//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Cubic.h"
using namespace Cephei::QL::Math::Interpolations;
#include <gen/QL/Math/Interpolation.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Interpolations::CCubic::CCubic (Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::DerivativeApproxEnum>^ da, Microsoft::FSharp::Core::FSharpOption<Boolean>^ monotonic, Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::BoundaryConditionEnum>^ leftCondition, Microsoft::FSharp::Core::FSharpOption<Double>^ leftConditionValue, Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::BoundaryConditionEnum>^ rightCondition, Microsoft::FSharp::Core::FSharpOption<Double>^ rightConditionValue) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCubic = NULL;
#endif
        QuantLib::CubicInterpolation::DerivativeApprox _da = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::DerivativeApproxEnum>::IsSome::get (da) ? (QuantLib::CubicInterpolation::DerivativeApprox)da->Value : QuantLib::CubicInterpolation::DerivativeApprox::Kruger); //10
        bool _monotonic = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (monotonic) ? (bool)ValueHelper::Convert (monotonic->Value) : false); //4
        QuantLib::CubicInterpolation::BoundaryCondition _leftCondition = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::BoundaryConditionEnum>::IsSome::get (leftCondition) ? (QuantLib::CubicInterpolation::BoundaryCondition)leftCondition->Value : QuantLib::CubicInterpolation::BoundaryCondition::SecondDerivative); //10
        QuantLib::Real _leftConditionValue = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (leftConditionValue) ? (QuantLib::Real)ValueHelper::Convert (leftConditionValue->Value) : 0.0); //4
        QuantLib::CubicInterpolation::BoundaryCondition _rightCondition = 
            (Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::BoundaryConditionEnum>::IsSome::get (rightCondition) ? (QuantLib::CubicInterpolation::BoundaryCondition)rightCondition->Value : QuantLib::CubicInterpolation::BoundaryCondition::SecondDerivative); //10
        QuantLib::Real _rightConditionValue = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (rightConditionValue) ? (QuantLib::Real)ValueHelper::Convert (rightConditionValue->Value) : 0.0); //4
        _ppCubic = new boost::shared_ptr<QuantLib::Cubic> (new QuantLib::Cubic ( _da,  _monotonic,  _leftCondition,  _leftConditionValue,  _rightCondition,  _rightConditionValue ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Interpolations::CCubic::CCubic (boost::shared_ptr<QuantLib::Cubic>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubic = NULL;
#endif
	_ppCubic = &childNative;
    
}
Cephei::QL::Math::Interpolations::CCubic::CCubic (QuantLib::Cubic& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCubic = NULL;
#endif
	_ppCubic = new boost::shared_ptr<QuantLib::Cubic> (&childNative);
    
    _CubicOwner = owner;
    
}

Cephei::QL::Math::Interpolations::CCubic::CCubic (CCubic^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubic = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCubic = new boost::shared_ptr<QuantLib::Cubic> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Interpolations::CCubic::CCubic (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubic = NULL;
#endif
	if (!t->IsSubclassOf(CCubic::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Interpolations::CCubic::CCubic (QuantLib::Handle<QuantLib::Cubic>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCubic = &childNative;
	_ppCubic = &static_cast<boost::shared_ptr<QuantLib::Cubic>>(childNative.currentLink());
    
    _CubicOwner = owner;
}
Cephei::QL::Math::Interpolations::CCubic::CCubic (QuantLib::Handle<QuantLib::Cubic> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCubic = &childNative;
	_ppCubic = &static_cast<boost::shared_ptr<QuantLib::Cubic>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Interpolations::CCubic::CCubic (QuantLib::Cubic childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCubic = NULL;
#endif
	_ppCubic = new boost::shared_ptr<QuantLib::Cubic> (new QuantLib::Cubic (childNative));
    
}
#endif

Cephei::QL::Math::Interpolations::CCubic::~CCubic ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCubic != NULL)
    {
	    delete _ppCubic;
        _ppCubic = NULL;
    }
}
Cephei::QL::Math::Interpolations::CCubic::!CCubic ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCubic != NULL)
    {
	    delete _ppCubic;
    }
}
QuantLib::Cubic& Cephei::QL::Math::Interpolations::CCubic::GetReference ()
{
    if (_ppCubic == NULL) throw gcnew NativeNullException ();
	return **_ppCubic;
}
boost::shared_ptr<QuantLib::Cubic>& Cephei::QL::Math::Interpolations::CCubic::GetShared ()
{
    if (_ppCubic == NULL) throw gcnew NativeNullException ();
	return *_ppCubic;
}
QuantLib::Cubic* Cephei::QL::Math::Interpolations::CCubic::GetPointer ()
{
    if (_ppCubic == NULL) throw gcnew NativeNullException ();
	return &**_ppCubic;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Cubic>& Cephei::QL::Math::Interpolations::CCubic::GetHandle ()
{
	if (_phCubic == NULL)
	{
		_phCubic = new Handle<QuantLib::Cubic> (*_ppCubic);
	}
	return *_phCubic;
}
#endif
bool Cephei::QL::Math::Interpolations::CCubic::HasNative () 
{
	return (_ppCubic != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Interpolations::ICubic^ Cephei::QL::Math::Interpolations::CCubic_Factory::Create (Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::DerivativeApproxEnum>^ da, Microsoft::FSharp::Core::FSharpOption<Boolean>^ monotonic, Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::BoundaryConditionEnum>^ leftCondition, Microsoft::FSharp::Core::FSharpOption<Double>^ leftConditionValue, Microsoft::FSharp::Core::FSharpOption<QL::Math::Interpolations::CubicInterpolation::BoundaryConditionEnum>^ rightCondition, Microsoft::FSharp::Core::FSharpOption<Double>^ rightConditionValue)
{
    return gcnew CCubic ( da,  monotonic,  leftCondition,  leftConditionValue,  rightCondition,  rightConditionValue);
}
